package mqq.view;

import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import mqq.controller.ChoiceController;
import mqq.controller.DrawEdgeController;
import mqq.model.DecisionLinesEvent;

/**
 * GUI object that holds everything
 * @author mqq
 */
public class ApplicationPanel extends JPanel {
	/** The entity object associated */
	DecisionLinesEvent event;
	
	/** Text fields for the choices */
	List<JTextField> choices = new ArrayList<JTextField>();
	
	/** The panel for drawing the edges */
	DrawingPanel drawing;
	
	/** The label for warnings */
	JLabel warning;
	
	/** Start Button */
	JButton startBtn;
	
	/** Whether the panel is active */
	boolean drawingActive = false;
	boolean choiceActive = true;
	
	public ApplicationPanel(DecisionLinesEvent e) {
		super();
		this.event = e;
		this.initialize();
	}
	
	/**
	 * Initialize the text fields and the drawing panel
	 */
	void initialize() {
		this.setLayout(null);
		int numChoices = this.event.getNumLines();
		for (int i = 0; i < numChoices; i++) {
			final int position = i;
			JTextField tf = new JTextField();
			tf.setBounds(new Rectangle(25 + i * 480 / (numChoices - 1), 25, 70, 20));
    		tf.addActionListener(new ActionListener() {
    			
    			@Override
    			public void actionPerformed(ActionEvent ae) {
    				new ChoiceController(event, position, ApplicationPanel.this).actionPerformed(ae);
    			}
    			
    		});
    		this.choices.add(i, tf);
			this.add(tf);
		}
		this.drawing = new DrawingPanel(this.event);
		this.drawing.addMouseListener(new MouseListener() {

			@Override
			public void mousePressed(MouseEvent e) {
				new DrawEdgeController(event, ApplicationPanel.this).mousePressed(e);
			}

			@Override
			public void mouseClicked(MouseEvent e) {
			}

			@Override
			public void mouseReleased(MouseEvent e) {
			}

			@Override
			public void mouseEntered(MouseEvent e) {
			}

			@Override
			public void mouseExited(MouseEvent e) {
			}

		});
		this.drawing.setBounds(0, 60, 600, 360);
		this.add(drawing);
		
		this.warning = new JLabel("");
		this.warning.setBounds(200, 430, 400, 50);
		this.add(warning);
		
		this.startBtn = new JButton("START");
		this.startBtn.setBounds(60, 450, 100, 30);
		startBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent ae) {
				performStartBtn();
			}
			
		});
		this.add(startBtn);
	}
	
	/**
	 * Things to do when the start button is clicked
	 * Disable the choice textfields and enable the drawing panel
	 */
	public void performStartBtn() {
		if (!event.isDone()) {
			if (event.isReady()) { 
				drawingActive = true;
				choiceActive = false;
				for (int i = 0; i < event.getNumLines(); i++) {
					choices.get(i).setEditable(false);
				}
			} else {
				setWarning("Please fill in all the blanks before click \"START\".");
			}
		}
	}
	
	/**
	 * Get a text field with given position
	 * @param position  The given position
	 * @return  The corresponding text field
	 */
	public JTextField getChoiceTextField(int position) {
		return this.choices.get(position);
	}
	
	/**
	 * Get the drawing panel
	 * @return  The drawing panel
	 */
	public DrawingPanel getDrawingPanel() {
		return this.drawing;
	}

	/**
	 * Set the warning text
	 * @param s  The new text to be set
	 */
	public void setWarning(String s) {
		this.warning.setText(s);
	}
	
	/**
	 * Determine whether drawing should be active
	 * @return  true if yes; false if no
	 */
	public boolean getDrawingActive() {
		return this.drawingActive;
	}
	
	/**
	 * Determine whether choice should be active
	 * @return  true if yes; false if no
	 */
	public boolean getChoiceActive() {
		return this.choiceActive;
	}

	/**
	 * Set whether drawing should be active
	 * @param b  true if yes; false if no
	 */
	public void setDrawingActive(boolean b) {
		this.drawingActive = b;
	}
	
	/**
	 * Set whether choice should be active
	 * @param b  true if yes; false if no
	 */
	public void setChoiceActive(boolean b) {
		this.choiceActive = b;
	}
	
}